package com.qf.text;

import com.qf.mybatis.CustomerDao;
import com.qf.mybatis.LinkmanDao;
import com.qf.mybatis.roleDao;
import com.qf.pojo.Customer;
import com.qf.pojo.Linkman;
import com.qf.pojo.role;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

//关系映射
public class text2 {
    SqlSession sqlSession;
    @Before
    public void before() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
         sqlSession = factory.openSession();
    }
    @Test
    //多对一
    public void findLinkmanAndCustomer(){
        LinkmanDao mapper = sqlSession.getMapper(LinkmanDao.class);
        List<Linkman> linkmanAndCustomer = mapper.findLinkmanAndCustomer();
        for (Linkman linkman: linkmanAndCustomer){
            System.out.println(linkman);
        }
    }
    @Test
    //一对多
    public void findCustomerAndLinkman(){
        CustomerDao mapper = sqlSession.getMapper(CustomerDao.class);
        List<Customer> customerAndLinkman = mapper.findCustomerAndLinkman();
        for (Customer customer:customerAndLinkman){
            System.out.println(customer);
        }
    }
    @Test
    //多对多  role是一 users是多
    public void findroleAndUsers(){
        roleDao mapper = sqlSession.getMapper(roleDao.class);
        List<role> duoAndDuo = mapper.findDuoAndDuo();
        for (role role:duoAndDuo){
            System.out.println(role);
        }
    }
}
